package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理和优惠券数据业务有关的接口
 */
@RestController
@Slf4j
@Tag(name="优惠券模块")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Operation(summary = "查询当前商家优惠券分页列表")
    @PostMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> pageList(@RequestBody CouponQuery query) throws BusinessException{
        log.debug("查询当前商家优惠券分页列表，入参：{}", query);
        PageResult<CouponVO> pageResult = couponService.pageList(query);
        log.debug("查询当前商家优惠券分页列表成功，出参：{}", pageResult);
        return new Result<>(pageResult);
    }


    @Operation(summary = "新增优惠券")
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> addCoupon(@RequestBody CouponParam couponParam)throws BusinessException{
        log.info("=== CouponController.addCoupon 开始 ===");
        log.info("新增优惠券，入参：{}", couponParam);
        
        try {
            couponService.save(couponParam);
            log.info("新增优惠券成功，入参：{}", couponParam);
            log.info("=== CouponController.addCoupon 结束 ===");
            return Result.success();
        } catch (Exception e) {
            log.error("新增优惠券失败：{}", e.getMessage(), e);
            throw e;
        }
    }

    @Operation(summary = "查询优惠券详情")
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> detail(@PathVariable("id") Integer id)
        throws BusinessException{
        log.info("=== CouponController.detail 开始 ===");
        log.info("查询优惠券详情，ID：{}", id);
        
        try {
            CouponVO couponVO = couponService.getById(id);
            log.info("查询优惠券详情成功，出参：{}", couponVO);
            log.info("=== CouponController.detail 结束 ===");
            return new Result<>(couponVO);
        } catch (Exception e) {
            log.error("查询优惠券详情失败：{}", e.getMessage(), e);
            throw e;
        }
    }



    @Operation(summary = "领取优惠券")
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)
        throws BusinessException{
        log.info("=== CouponController.receiveCoupon 开始 ===");
        log.info("领取优惠券，入参：{}", param);
        
        try {
            couponService.receiveCoupon(param);
            log.info("领取优惠券成功，入参：{}", param);
            log.info("=== CouponController.receiveCoupon 结束 ===");
            return Result.success();
        } catch (Exception e) {
            log.error("领取优惠券失败：{}", e.getMessage(), e);
            throw e;
        }
    }


    @Operation(summary = "用户领取优惠券列表")
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> receiveList(@RequestBody UserCouponQuery couponQuery)throws BusinessException{
        log.info("=== CouponController.receiveList 开始 ===");
        log.info("查询用户领取优惠券列表，入参：{}", couponQuery);
        
        try {
            PageResult<UserCouponsVO> pageResult = couponService.getUserCouponList(couponQuery);
            log.info("查询用户领取优惠券列表成功，出参：{}", pageResult);
            log.info("=== CouponController.receiveList 结束 ===");
            return new Result<>(pageResult);
        } catch (Exception e) {
            log.error("查询用户领取优惠券列表失败：{}", e.getMessage(), e);
            throw e;
        }
    }


    @Operation(summary = "查看优惠券码的二维码图片")
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> receiveDetail(@RequestBody UserCouponCodeQuery query)
        throws BusinessException{
        log.info("=== CouponController.receiveDetail 开始 ===");
        log.info("查看优惠券二维码，入参：{}", query);
        
        try {
            UserCouponsVO result = couponService.getCouponQrCode(query);
            log.info("查看优惠券二维码成功，出参：{}", result);
            log.info("=== CouponController.receiveDetail 结束 ===");
            return new Result<>(result);
        } catch (Exception e) {
            log.error("查看优惠券二维码失败：{}", e.getMessage(), e);
            throw e;
        }
    }

    @Operation(summary = "优惠券核销")
    @PostMapping("/marketing/user/verify/{couponCode}")
    public Result<Void> verifyCoupon(@PathVariable("couponCode") String couponCode)
        throws BusinessException{
        log.info("=== CouponController.verifyCoupon 开始 ===");
        log.info("优惠券核销，优惠券编码：{}", couponCode);
        
        try {
            couponService.verifyCoupon(couponCode);
            log.info("优惠券核销成功，优惠券编码：{}", couponCode);
            log.info("=== CouponController.verifyCoupon 结束 ===");
            return Result.success();
        } catch (Exception e) {
            log.error("优惠券核销失败：{}", e.getMessage(), e);
            throw e;
        }
    }

}
